package dao;

import com.alibaba.fastjson.JSON;
import pojo.product.ProductTypeEntity;
import utils.DataFileName;
import utils.DataUtils;
import utils.JsonUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Limohan
 */
public class ProductTypeDao {
    public void saveType(ProductTypeEntity type) {
        String json = JSON.toJSONString(type);
        try {
            DataUtils.writeData(DataFileName.PRODUCTTYPE.toString(), json);
        } catch (NullPointerException | IOException e) {
            e.printStackTrace();
        }
    }
    public List<ProductTypeEntity> findUserList() throws IOException {
        String json = DataUtils.readData(DataFileName.PRODUCTTYPE.getName());
        if(json.length() == 0){
            return null;
        }
        List<ProductTypeEntity> productTypeEntities = new ArrayList<>();
        String[] types = json.split("/");
        for(String str:types){
            ProductTypeEntity typeEntity = JsonUtil.jsonToObject(str,ProductTypeEntity.class);
            productTypeEntities.add(typeEntity);
        }
        return productTypeEntities;
    }
    public List<ProductTypeEntity> findTypeByName(String name)throws IOException{
        List<ProductTypeEntity> productTypeEntities= findUserList();
        List<ProductTypeEntity> productTypeEntities1 = new ArrayList<>();
        if(productTypeEntities!=null){
            for(ProductTypeEntity productTypeEntity:productTypeEntities){
                if(name.equals(productTypeEntity.getType())){
                    productTypeEntities1.add(productTypeEntity);
                }
            }
            return productTypeEntities1;
        }
        return null;

    }
    public String getMaxId() throws IOException {
        List<ProductTypeEntity> productTypeEntities = findUserList();
        int max = 0;
        if (productTypeEntities == null || productTypeEntities.size() == 0) {
            return "1";
        } else {
            for (ProductTypeEntity productTypeEntity : productTypeEntities) {
                if (Integer.parseInt(productTypeEntity.getId()) > max) {
                    max = Integer.parseInt(productTypeEntity.getId());
                }
            }
            return String.valueOf(max + 1);
        }
    }
    public void deleteType(String id) throws IOException {
        List<ProductTypeEntity> productTypeEntities = findUserList();
        productTypeEntities.removeIf(productTypeEntity -> productTypeEntity.getId().equals(id));
        DataUtils.clearData(DataFileName.PRODUCTTYPE.toString());
        for(ProductTypeEntity productTypeEntity:productTypeEntities){
            this.saveType(productTypeEntity);
        }
    }
    public boolean isType(String Name) throws IOException {
        List<ProductTypeEntity> productTypeEntities = findUserList();
        if(productTypeEntities!=null){
            for(ProductTypeEntity typeEntity:productTypeEntities){
                if(Name.equals(typeEntity.getType())){
                    return true;
                }
            }
        }
        return false;
    }
    public ProductTypeEntity findUser(String account) throws IOException {
        List<ProductTypeEntity>  productTypeEntityList = findUserList();
        if(productTypeEntityList!=null){
            for(ProductTypeEntity productTypeEntity:productTypeEntityList){
                if(account.equals(productTypeEntity.getType())){
                    return productTypeEntity;
                }
            }
        }
        return null;
    }
    public void changeType(ProductTypeEntity productTypeEntity,String newType) throws IOException {
        List<ProductTypeEntity> productTypeEntityList = findUserList();
        for(ProductTypeEntity productTypeEntity1:productTypeEntityList){
            if(productTypeEntity.getId().equals(productTypeEntity1.getId())){
                productTypeEntity1.setType(newType);
            }
        }
        DataUtils.clearData(DataFileName.PRODUCTTYPE.toString());
        for(ProductTypeEntity typeEntity:productTypeEntityList){
            this.saveType(typeEntity);
        }
    }
}
